package controller;

import dao.CommodityDao;
import dao.CommtypeDao;
import entity.Commodity;
import entity.Lin;
import entity.Record;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CommodityService;
import service.LinService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/commodity")
@Scope("prototype")

public class CommodityCon {
    @Resource(name="commodityService")
    private CommodityService commodityService;
   @Resource(name = "commodityDao")
    private CommodityDao commodityDao;

    @Resource(name="linService")
    private LinService linService;

    @RequestMapping("/tt")
    public String tt(){
        return "WEB-INF/jsp/KuCunGuanLi/kucun";
    }


    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> list(String lei,String name,String chang,int pageNumber,int pageSize){

        Commodity commodity = new Commodity();
        commodity.setName(name);
        try {
            commodity.setWarehouseid(Integer.parseInt(chang));
        }catch (Exception e){
            commodity.setWarehouseid(-1);
        }

        Map<String,Object> map = new HashMap<String,Object>() ;
        Map<String,Object> map1 = new HashMap<String,Object>() ;
        map1.put("lei",lei);
        map1.put("name",name) ;
        map1.put("warehouseid",chang);
        map1.put("offset",(pageNumber-1) * pageSize) ;
        map1.put("limit",pageSize) ;


        List<Commodity> list = commodityService.queryByName(map1) ;


        map.put("total",commodityService.count(commodity)) ;
        map.put("rows",list) ;


        return map;
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Commodity> list(){
        return commodityService.list();
    }

    @RequestMapping("/Qtiaozhuan")


    public String tiaozhuan(Model model){
        return "WEB-INF/jsp/tongjibaobiao/huopingcaigoutongji";
    }

    @RequestMapping("/QqueryList")
    @ResponseBody
    public Map<String,Object> QqueryList(String typeid,String name,int pageNumber,int pageSize,String firstDate,String secondDate){

        Map<String,Object> map = new HashMap<String,Object>() ;
        Map<String,Object> map1 = new HashMap<String,Object>() ;

        Commodity commodity = new Commodity();
        int id = -1;
        if(typeid!=null && typeid !=""){
            id = Integer.parseInt(typeid);
        }

        commodity.setTypeid(id);
        commodity.setName(name);


        map1.put("typeid",typeid);
        map1.put("name",name) ;
        map1.put("secondDate",secondDate);
        map1.put("firstDate",firstDate) ;
        map1.put("offset",(pageNumber-1) * pageSize) ;
        map1.put("limit",pageSize) ;

        List<Commodity> list =commodityService.QqueryList(map1) ;

        map.put("total",commodityDao.QgetCount(commodity)) ;
        map.put("rows",list) ;


        return map;
    }





    @RequestMapping("/c")
    @ResponseBody
    public int chanumber(){

        String lin = linService.chanumber();
        int com = commodityService.chanumber();

        if(lin!=null){
            int l = Integer.parseInt(lin);
            if(l>com){
                return l;
            }else if(l<com){
                return com;
            }else if(l==com){
                return com;
            }
        }else{
            return com;
        }

        return 0;
    }
    @RequestMapping("/comprehensive")
    @ResponseBody
    public int comprehensive(int ckid){
        System.out.println("0");
        List<Lin> list = linService.list3();
        Commodity c = null;

        for(int i = 0;i<list.size();i++){

            c = new Commodity();
            c.setNumber(list.get(i).getNumber());
            c.setWarehouseid(ckid);



            c = commodityService.cha(c);


            if(c==null){
                c = new Commodity();

                c.setNumber(list.get(i).getNumber());


                c.setName(list.get(i).getName());

                c.setCompanyid(list.get(i).getCompanyid());


                c.setSpec(list.get(i).getSpec());
                c.setColorid(list.get(i).getColorid());
                c.setBprice(list.get(i).getBprice());
                c.setEprice(list.get(i).getBprice()+1);
                c.setStock(list.get(i).getSum());
                c.setWarehouseid(ckid);



                int j = commodityService.add(c);
                if(j!=0){
                    System.out.println("加成功");
                }else{
                    System.out.println("加失败");
                }

            }else{
                c.setBprice(list.get(i).getBprice());
                c.setStock(c.getStock()+list.get(i).getSum());

                int j = commodityService.upd(c);
                if(j!=0){
                    System.out.println("改成功");
                }else{
                    System.out.println("改失败");
                }
            }

        }

        return 0;
    }

    @RequestMapping("/upd")
    @ResponseBody
    public Map<String,Object> upd(Commodity commodity){

        int i = commodityService.upd2(commodity);

        return list(null,null,null,1,9);
    }



    @RequestMapping(value="/tui", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String tui(){


        List<Lin> list = linService.list2();
        int s = 0;
        for (int i = 0;i<list.size();i++){

            Commodity c = commodityService.chanumber1(list.get(i).getNumber());

            c.setStock(c.getStock()-list.get(i).getSum());
            if(c.getStock()<=-1){
                return c.getName()+"的数量超过了库存数量！";
            }

            int ss = commodityService.upd(c);

            if(ss!=-1){
                s++;
            }

        }

        return "123";
    }





}
